﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WisDom.SmartTMS.Dto;
using WisDom.SmartTMS.Entity;
using WisDom.SmartTMS.InterFace;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
using Abp.Application.Services.Dto;
using Volo.Abp.EventBus.Local;
using WisDom.SmartTMS.Events;
using WisDom.SmartTMS.Data;

namespace WisDom.SmartTMS.Services
{
    /// <summary>
    /// 人事入职AppService实现类
    /// </summary>
    public class PersonnelEntryAppService : ApplicationService, IPersonnelEntryAppService
    {
        /// <summary>a
        /// 注入上下文
        /// </summary>
        private readonly IRepository<PersonnelEntry, Guid> _personnelEntries;
        private readonly ILocalEventBus _localEventBus;
        private readonly IBusinessAuditRepository _businessAudit;

        public PersonnelEntryAppService(IRepository<PersonnelEntry, Guid> personnelEntries, ILocalEventBus localEventBus, IBusinessAuditRepository businessAudit)
        {
            _personnelEntries = personnelEntries;
            _localEventBus = localEventBus;
            _businessAudit = businessAudit;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="leaveDealDtoCreate"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PersonnelEntryDtoCreate> Add([FromForm] PersonnelEntryDtoCreate personnelEntryDto)
        {
            PersonnelEntry personnel = new(
               GuidGenerator.Create(),
               personnelEntryDto.EntryName,
               personnelEntryDto.DepId,
               personnelEntryDto.Position,
               personnelEntryDto.ComeTimes,
               personnelEntryDto.CreateTimes,
               personnelEntryDto.ContracTimes,
               personnelEntryDto.ApprovalSeate,
               personnelEntryDto.Principal,
               personnelEntryDto.Approver,
               CurrentUser.Id
               );

            var result = await _personnelEntries.InsertAsync(personnel);

            //入职信息添加到审批
            Audit b = new(
                GuidGenerator.Create(),
                GuidGenerator.Create(),
                Enums.BusinessType.PersonnelEntry,
                DateTime.Now,
                GuidGenerator.Create()
                );
            _ = _businessAudit.InsertAsync(b);



            //②发送邮件(用本地事件总线）
            await _localEventBus.PublishAsync(
                    new VehicleAddedEvent   //事件类
                    {
                        VehicleId = result.Id,
                        Brand = result.EntryName,
                        Company = result.DepId,
                        Driver = result.Position
                    }
                );
            return ObjectMapper.Map<PersonnelEntry, PersonnelEntryDtoCreate>(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task Delete(Guid id)
        {
            await _personnelEntries.DeleteAsync(id);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PersonnelEntryDto> Edit(Guid id)
        {
            var data = await _personnelEntries.GetAsync(id);
            return ObjectMapper.Map<PersonnelEntry, PersonnelEntryDto>(data);
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<PagedResultDto<PersonnelEntryDto>> PaymentShow(PagedAndSortedResultRequestDto input)
        {
            //创建一个查询对象
            var query = _personnelEntries
                .OrderBy(input.Sorting);
            //总条数
            var totalCount = await query.CountAsync();
            //从仓储获取实体数据对象
            List<PersonnelEntry> leaveDeal = await query
                .Skip(input.SkipCount)
                .Take(input.MaxResultCount).ToListAsync();
            //进行实体转换Dto
            List<PersonnelEntryDto> leaveDealDto =
                ObjectMapper.Map<List<PersonnelEntry>, List<PersonnelEntryDto>>(leaveDeal);
            //返回结果
            return new PagedResultDto<PersonnelEntryDto>(totalCount, leaveDealDto);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PersonnelEntryDto> Upt([FromForm] PersonnelEntryDto personnelEntry)
        {
            PersonnelEntry m = new(

               personnelEntry.Id,
               personnelEntry.EntryName,
               personnelEntry.DepId,
               personnelEntry.Position,
               personnelEntry.ComeTimes,
               personnelEntry.CreateTimes,
               personnelEntry.ContracTimes,
               personnelEntry.ApprovalSeate,
               personnelEntry.Principal,
               personnelEntry.Approver,
               CurrentUser.Id
                );
            var result = await _personnelEntries.UpdateAsync(m);
            return ObjectMapper.Map<PersonnelEntry, PersonnelEntryDto>(result);
        }
    }
}
